/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.util.ArrayList;
import java.util.List;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JToggleButton;

/**
 *
 * @author Administrator
 */
public class LoadFrame extends BaseFrame{
    List<JToggleButton> buttons = new ArrayList<JToggleButton>();
    List<Question> qs;
    Question current;
    ButtonGroup bg;
    private int i = 0, score = 0;
    
    
    public LoadFrame(List<Question> qs) {
        super("Boa Sorte!");
        this.qs = qs;
        addQuestion(qs.get(0));
        if(qs.size()==1) forward.setText("Finished!");
        this.setVisible(true);
        // ..................
    }
    
    private void addQuestion(Question q) {
        quests.removeAll();
        this.pack();
        buttons.clear();
        bg = new ButtonGroup();
        quests.add(Box.createVerticalStrut(3));
        quests.add(new JLabel(q.getDescription()));
        quests.add(Box.createVerticalStrut(5));
        Class<? extends JToggleButton> cl = (q.getType()==Question.Single_Selection)?JRadioButton.class:JCheckBox.class;
//        JScrollPane js = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        RandomSet<String> rs = new RandomSet<String>(q.getOptions());
        Box jsb = Box.createVerticalBox();
        for(String s : rs) {
            try {
                JToggleButton rb = cl.getConstructor(String.class).newInstance(s);
                rb.setVisible(true);
                rb.setActionCommand(s);
                if(cl == JRadioButton.class) bg.add(rb);
                buttons.add(rb);
                Box bb = Box.createHorizontalBox();
                bb.add(rb); bb.add(Box.createHorizontalStrut(3));
//                bb.add(new JLabel(s));
                jsb.add(bb);
                jsb.add(Box.createVerticalStrut(3));
            } catch (Exception e) { e.printStackTrace(); System.exit(1); }
        }
//        js.add(jsb);
        quests.add(jsb);
        quests.add(Box.createVerticalStrut(2));
        current = q;
        this.pack();
    }

    @Override
    public void loadNext() {
//        List<AbstractButton> l = Collections.list(bg.getElements());
        List<String> sts = new ArrayList<String>();
        
        for(JToggleButton b : buttons) {
//            JToggleButton b = (JToggleButton) bb;
            if(b.isSelected()) sts.add(b.getActionCommand());
        }
        
        if(sts.isEmpty()) { 
            JOptionPane.showMessageDialog(this, "Selecione a resposta! Pelo menos chute!", "Mano, vc é muito louco??", JOptionPane.WARNING_MESSAGE);
            return;
        }
        
        System.out.println("Chose-> " + sts);  //Debugging
        System.out.println("Correct-> " + current.getCorrectAnswers() + "\n"); //Debugging
        
        if(current.isCorrect(sts)) {
            JOptionPane.showMessageDialog(this, "Parabéns cara!", "Você acertou!", JOptionPane.INFORMATION_MESSAGE);
            score++;
        } else {
            boolean b = current.isSingleSelection();
            String pl = b? "": "s";
            String corrects = current.getCorrectAnswers().toString();
            corrects = corrects.substring(1, corrects.length()-1);
            JOptionPane.showMessageDialog(this, "Oops... A" + pl + " alternativa" + pl + " certa" + pl + (b?" é: ":" são: ") + corrects , "Você esta... Errado!",JOptionPane.INFORMATION_MESSAGE);   
        }
        
        final int s = qs.size()-1;
        if(i==s) {
            JOptionPane.showMessageDialog(this, "Finalmente... Você acertou " + score + " de " + (s+1), "Acabou!!!!", JOptionPane.INFORMATION_MESSAGE);
            Prova.mf.setVisible(true);
            this.dispose();
            return;
        }
        addQuestion(qs.get(++i));
        if(i==s) {
            forward.setText("Finished!");
        }
    }

    @Override
    public void loadPrevious() {
        addQuestion(qs.get(--i));
        activateForward();
        if(i==0) deactivateBack();
    }
}
